{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 以下三行是汉化过程中测试用代码，学习者在自己的环境中应删除或注释掉\n",
    "导入 sys\n",
    "导入 os\n",
    "sys.path.追加(os.路径.绝对路径(r'G:\\grasspy_zwmodules\\bs4\\zwbs\\src'))\n",
    "\n",
    "导入 网络请求\n",
    "从 蜘蛛网 导入 〇蜘蛛网\n",
    "\n",
    "响应 = 网络请求.查_get(\"https://www.baidu.com\")\n",
    "蜘蛛网 = 〇蜘蛛网(响应.内容, 特性=\"html.parser\")  # 代表被解析的文档整体"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[' ',\n",
       " <a href=\"http://home.baidu.com\">关于百度</a>,\n",
       " ' ',\n",
       " <a href=\"http://ir.baidu.com\">About Baidu</a>,\n",
       " ' ']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 游走蜘蛛网（或文档树）\n",
    "# '内容々' 和 '所有子元素'\n",
    "蜘蛛网.p.内容々"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<list_iterator at 0x10a72d60250>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "蜘蛛网.p.所有子元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " \n",
      "<a href=\"http://home.baidu.com\">关于百度</a>\n",
      " \n",
      "<a href=\"http://ir.baidu.com\">About Baidu</a>\n",
      " \n"
     ]
    }
   ],
   "source": [
    "取 子元素 于 蜘蛛网.p.所有子元素:\n",
    "    打印(子元素)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " \n",
      "<a href=\"http://home.baidu.com\">关于百度</a>\n",
      "关于百度\n",
      " \n",
      "<a href=\"http://ir.baidu.com\">About Baidu</a>\n",
      "About Baidu\n",
      " \n"
     ]
    }
   ],
   "source": [
    "# 所有子孙元素\n",
    "取 元素 于 蜘蛛网.p.所有子孙元素:\n",
    "    打印(元素)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'百度一下，你就知道'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字符串\n",
    "# 如果一个标签只有一个子元素，而且该子元素是一个可导航字符串，或者该子元素\n",
    "# 是另一个标签，且该标签只要一个字符串，那么可通过 .字符串 获取该字符串\n",
    "蜘蛛网.title.字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 如果一个标签内有一个以上的字符串，那么使用 .字符串 将得到空值\n",
    "蜘蛛网.noscript.字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " \n",
      "登录\n",
      " \n"
     ]
    }
   ],
   "source": [
    "# 所有字符串，所有经修剪字符串\n",
    "取 字符串 于 蜘蛛网.noscript.所有字符串:\n",
    "    打印(字符串)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "登录\n"
     ]
    }
   ],
   "source": [
    "取 字符串 于 蜘蛛网.noscript.所有经修剪字符串:  # 会忽略空白字符串\n",
    "    打印(字符串)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<div id=\"ftConw\"> <p id=\"lh\"> <a href=\"http://home.baidu.com\">关于百度</a> <a href=\"http://ir.baidu.com\">About Baidu</a> </p> <p id=\"cp\">©2017 Baidu <a href=\"http://www.baidu.com/duty/\">使用百度前必读</a>  <a class=\"cp-feedback\" href=\"http://jianyi.baidu.com/\">意见反馈</a> 京ICP证030173号  <img src=\"//www.baidu.com/img/gs.gif\"/> </p> </div>"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 父元素，所有父元素\n",
    "段 = 蜘蛛网.p\n",
    "# 段\n",
    "段.父元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<title>百度一下，你就知道</title>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "抬头 = 蜘蛛网.title\n",
    "抬头.字符串.父元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "取 元素 于 抬头.所有父元素:\n",
    "    打印(元素)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<a class=\"mnav\" href=\"https://www.hao123.com\" name=\"tj_trhao123\">hao123</a>"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 上一兄弟元素 和 下一兄弟元素\n",
    "# 之前所有兄弟元素 和 之后所有兄弟元素\n",
    "蜘蛛网.a.上一兄弟元素\n",
    "蜘蛛网.a.下一兄弟元素.下一兄弟元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " \n"
     ]
    }
   ],
   "source": [
    "取 元素 于 蜘蛛网.a.之前所有兄弟元素:\n",
    "    打印(元素)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "取 元素 于 蜘蛛网.a.之后所有兄弟元素:\n",
    "    打印(元素)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 上一元素 和 下一元素\n",
    "蜘蛛网.a.上一元素\n",
    "蜘蛛网.a.上一元素.上一元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'新闻'"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "蜘蛛网.a.下一元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<a class=\"mnav\" href=\"https://www.hao123.com\" name=\"tj_trhao123\">hao123</a>"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "蜘蛛网.a.下一元素.下一元素.下一元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 之前所有元素 和 之后所有元素\n",
    "蜘蛛网.a.之前所有元素\n",
    "蜘蛛网.a.之后所有元素"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.1 ('.venv': venv)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.1"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "9f23957215ae7e6101af7478a3aa6d946ec85d2106801d4efdfa4b8f6a5e1ee8"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
